<?php
namespace Common\Model;

/**
 * 客栈预定端信息表
 * @modify trainyao 2015-12-31 创建
 * @index hoteldetail(`hotelentity_id`)
 */

Class HotelDetailModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_hotel_detail';
    protected $field = 'hotelentity_id,name,description,special,specialimageurl,coverimageurl,contact,mobile,telephone,wechat,hotline,address,longtitude,latitude,ordertimeout,bookmode,enable,opened,enddate,certified,certifiedenddate,click,logoimageurl,areacode,minusenable,sharekey';

    
    /**
     * 添加客栈上架信息
     * @modify trainyao 2015-12-31      创建
     * @param  integer   $hotelId        客栈id
     */
    public function addInfo($hotelId) {
        $data = array(
            'hotelentity_id' => $hotelId
        );
        
        return $this->add($data) === false ? false : true;
    }
    
    /**
     * 获取客栈预定信息
     * @modify trainyao 2015-12-31 创建
     * @modify trainyao 2016-05-10 改为可获取多个
     * @param  mixin   $hotelId   客栈id
     * @return array
     */
    public function getInfoByHotel($hotelId) {
        $option = array();
        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') === false) {
            $option['hotelentity_id'] = $hotelId;
        } else {
            $option['hotelentity_id'] = array('in', $hotelId);
        }
        
        $option['isdelete'] = NOT_DELETE;
        if (strpos($hotelId, ',') === false) {
            $data = $this->where($option)->field($this->field)->find();
        } else {
            $data = $this->where($option)->field($this->field)->select();
        }
        
        return empty($data) ? array() : $data;
    }

     /**
     * 获取客栈预定信息
     * @modify wenhui 2017-02-28 创建
     * @param  mixin   $hotelId   客栈id
     * @return array
     */
    public function getInfoByHotels($hotelId) {
        $option = array();

        $option['enable'] = ENABLE;
        $option['opened'] = ENABLE;
        $option['enddate'] = array('egt', date('Y-m-d'));

        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }

        if(strpos($hotelId,',')){
            $option['hotelentity_id'] = array('in', $hotelId);
        }else{
            $option['hotelentity_id'] = $hotelId;
        }

        $option['isdelete'] = NOT_DELETE;
        $data = $this->where($option)->field($this->field)->select();

        return empty($data) ? array() : $data;
    }
    
    /**
     * 保存客栈上架信息
     * @modify trainyao 2015-12-31      创建
     * @param  integer   $hotelId        客栈id
     * @param  string   $name           客栈微预定名字
     * @param  string   $description    客栈描述
     * @param  string   $special        客栈个人推荐内容
     * @param  string   $specialImageUrl 个人推荐图片url           
     * @param  string   $coverImageUrl 封面图           
     * @param  string   $contact         联系人
     * @param  string   $mobile          联系手机
     * @param  string   $telephone       前台电话
     * @param  string   $wechat          联系人微信号
     * @param  string   $hotline         投诉热线
     * @param  string   $address         地址
     * @param  string   $longtitude      经度
     * @param  string   $latitude        纬度
     * @param  string   $orderTimeOut    订单过期时间
     * @param  int   $minusEnable    会员房费立减优惠开关
     */
    public function saveInfo($hotelId, $name = null, $description = null, $special = null, $specialImageUrl = null, $coverImageUrl = null, $contact = null, $mobile = null, $telephone = null, $wechat = null, $address = null, $longtitude = null, $latitude = null, $orderTimeOut = null, $bookMode = null, $hotline = null, $logoImageUrl = null, $areaCode = null, $minusEnable = null) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'isdelete' => NOT_DELETE,
        );
        $data = array();
        if (!is_null($name)) {
            $data['name'] = $name;
        }
        if (!is_null($description)) {
            $data['description'] = $description;
        }
        if (!is_null($special)) {
            $data['special'] = $special;
        }
        if (!is_null($logoImageUrl)) {
            $data['logoimageurl'] = $logoImageUrl;
        }
        if (!is_null($specialImageUrl)) {
            $data['specialimageurl'] = $specialImageUrl;
        }
        if (!is_null($coverImageUrl)) {
            $data['coverimageurl'] = $coverImageUrl;
        }
        if (!is_null($contact)) {
            $data['contact'] = $contact;
        }
        if (!is_null($mobile)) {
            $data['mobile'] = $mobile;
        }
        if (!is_null($telephone)) {
            $data['telephone'] = $telephone;
        }
        if (!is_null($wechat)) {
            $data['wechat'] = $wechat;
        }
        if (!is_null($hotline)) {
            $data['hotline'] = $hotline;
        }
        if (!is_null($address)) {
            $data['address'] = $address;
        }
        if (!is_null($longtitude)) {
            $data['longtitude'] = $longtitude;
        }
        if (!is_null($latitude)) {
            $data['latitude'] = $latitude;
        }
        if (!is_null($orderTimeOut)) {
            $data['ordertimeout'] = $orderTimeOut;
        }
        if (!is_null($bookMode)) {
            $data['bookmode'] = $bookMode;
        }
        if (!is_null($areaCode)) {
            $data['areacode'] = $areaCode;
        }
        if (!is_null($minusEnable)) {
            $data['minusenable'] = $minusEnable;
        }

        //$data['areacode'] = $areaCode;

        return $this->where($option)->save($data) === false ? false : true;
    }
    /**
     * 实时搜索客栈
     * @modify wenhui 2017-01-04 创建
     * @param  int   $city   地区区号
     * @param  int   $keyWord   关键字
     * @return array
     */
    public function searchHotel($city, $keyWord) {
        if (is_null($city) && is_null($keyWord)) {
            // 获取6条热门客栈信息(按点击量排序) 不取已经禁用的客栈 即 status = 1
            $sql="select * from bk_hotel_detail where isdelete = 0 and hotelentity_id in(select id from tb_hotel_entity where status =0) order by click desc limit 6";
            $dataResult = $this->query($sql);

            return empty($dataResult) ? array() : $dataResult;
        }

        if (!is_null($city)) {
            // 获取搜索客栈信息(按城市区号 关键词搜索) 不取已经禁用的客栈 即 status = 1
            $sql="select * from bk_hotel_detail where isdelete = 0 and hotelentity_id in (select id from tb_hotel_entity where city_id = $city and status =0) and name like '%$keyWord%'";
            $dataResult = $this->query($sql);

            return empty($dataResult) ? array() : $dataResult;
        }

        // 获取搜索客栈信息(按关键词搜索) 不取已经禁用的客栈 即 status = 1
        $sql="select * from bk_hotel_detail where isdelete = 0 and hotelentity_id in (select id from tb_hotel_entity where status =0) and name like '%$keyWord%'";
        $dataResult = $this->query($sql);

        return empty($dataResult) ? array() : $dataResult;
    }

    public function saveClick($hotelId) {
        $sql="update bk_hotel_detail set click = (click+1) where hotelentity_id ={$hotelId}";
        $this->query($sql);
	}
    /**
     * 禁用/启用微预定
     * @modify trainyao 2016-07-28 修改为可同时修改多个客栈
     * @param  mixin   $hotelId   客栈ID
     * @param  integer   $isdelete    需要修改为该状态0=启用,1=禁用
     * @return boolean   执行结果
     */
    public function toggleUse($hotelId, $isdelete) {
        $option = array();
        $data = array();
        $option['hotelentity_id'] = $hotelId;
        $data['isdelete'] = $isdelete;

        return $this->where($option)->save($data) === false ? false : true;
	}
	
	public function saveShareStatus($hotelId, $status) {
	    $option = array(
            'hotelentity_id' => $hotelId,
            'isdelete' => NOT_DELETE,
        );
	    $data = array(
	        'sharekey' => $status,
	    );
	    return $this->where($option)->save($data) === false ? false : true;
	}
}
